package cn.zs.groupmarket.types.design.framework.tree;

import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
 * @Description:
 * @Auther: zs
 * @Date: 2024/12/23/16:08
 */
public abstract class AbstractMultiThreadStrategyRouter<T,D,R> implements StrategyMapper<T,D,R>, StrategyHandle<T,D,R>{

    @Getter
    @Setter
    protected StrategyHandle<T,D,R> defaultStrategyHandle = StrategyHandle.DEFAULT;
    public R router(T requestParameter,D dynamicContext) throws Exception {
        StrategyHandle<T,D,R> handle = get(requestParameter,dynamicContext);
        if (handle == null) {
            return defaultStrategyHandle.apply(requestParameter,dynamicContext);
        }
        return handle.apply(requestParameter,dynamicContext);
    }

    public R apply(T requestParameter,D dynamicContext) throws Exception {
        // 异步加载数据
        multiThread(requestParameter,dynamicContext);
        // 业务流程处理
        return doApply(requestParameter,dynamicContext);
    }

    /**
    *
    * 业务处理
    * */
    protected abstract R doApply(T requestParameter, D dynamicContext) throws Exception;

    /**
     *
     * 异步加载数据
     * */
    protected  abstract void multiThread(T requestParameter, D dynamicContext) throws ExecutionException, InterruptedException, TimeoutException;



}
